/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.ux.plan;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.data.dao.PlanDAO;
import com.softwareinlife.vision.android.data.dao.VisionDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.PlanCreateException;
import com.softwareinlife.vision.android.data.model.PlanModel;
import com.softwareinlife.vision.android.data.model.VisionModel;
import com.softwareinlife.vision.android.ux.util.SharedPreferenceUtility;

public class CreatePlanActivity extends Activity {

	/**
	 * Vision Model
	 */
	private VisionModel vision = null;

	/**
	 * Vision Title
	 */
	private TextView visionTitleTextView = null;

	// Member Variables //////////////////////////////

	/**
	 * Title Text View
	 */
	private EditText titleEditText = null;

	/**
	 * Desc Edit Text
	 */
	private EditText descEditText = null;

	// CREATE BUTTON /////////////////////////////////////

	/**
	 * Button of Create
	 */
	private Button createButton = null;

	/**
	 * Click Listener for Create Button
	 */
	private OnClickListener createButtonClickListener = null;

	// CANCEL BUTTON /////////////////////////////////////

	/**
	 * Button of Cancel
	 */
	private Button cancelButton = null;

	/**
	 * Click Listener for Create Button
	 */
	private OnClickListener cancelButtonClickListener = null;

	/**
	 * Constructor
	 */
	public CreatePlanActivity() {
		super();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Load Data
		loadData();

		// Set View
		setContentView(R.layout.plan_create);

		// Initialize Members
		initMemembers();

		// Initialize Actions
		initActions();
	}

	/**
	 * Initialize Members
	 */
	private void initMemembers() {
		// Title
		titleEditText = (EditText) findViewById(R.id.plan_title_EditText);

		// Statement
		descEditText = (EditText) findViewById(R.id.plan_desc_EditText);

		// Start Date
		visionTitleTextView = (TextView) findViewById(R.id.visionTitleTextView);
		visionTitleTextView.setText("(" + vision.getTitle() + ")");

		// Create Button
		createButton = (Button) findViewById(R.id.plan_create_Button);

		// Cancel Button
		cancelButton = (Button) findViewById(R.id.plan_cancel_Button);
	}

	/**
	 * Initialize Actions
	 */
	private void initActions() {

		// CREATE BUTTON //////////////////////////////////

		createButtonClickListener = new OnClickListener() {
			public void onClick(View v) {
				createPlanHandler();
				changePlanListScreen();
			}
		};

		createButton.setOnClickListener(createButtonClickListener);

		// CANCEL BUTTON //////////////////////////////////
		cancelButtonClickListener = new OnClickListener() {
			public void onClick(View v) {

				changePlanListScreen();
			}
		};

		cancelButton.setOnClickListener(cancelButtonClickListener);
	}

	/**
	 * Change Plan List Screen
	 */
	protected void changePlanListScreen() {
		Intent intent = new Intent(this, ReadPlanActivity.class);
		startActivity(intent);
	}

	/**
	 * Create Plan Handler
	 * 
	 */
	protected void createPlanHandler() {
		PlanDAO planDAO = null;

		try {
			// Create Model
			PlanModel model = new PlanModel();
			model.setTitle(titleEditText.getText().toString());
			model.setDesc(descEditText.getText().toString());
			model.setVision(vision);
			model.setUser(SharedPreferenceUtility
					.getUserModel(getApplicationContext()));

			// Now
			Date now = new Date();
			model.setCreatedDate(now);
			model.setModifiedDate(now);

			// Create DAO
			planDAO = new PlanDAO(getApplicationContext());
			// Create Operation
			planDAO.create(model);

		} catch (DatabaseConnectionException e) {
			e.printStackTrace();
		} catch (PlanCreateException e) {
			e.printStackTrace();
		}

		planDAO.close();
	}

	/**
	 * Load List Data
	 */
	private void loadData() {
		// Get Vision
		vision = SharedPreferenceUtility
				.getVisionModel(getApplicationContext());
	}
}